package com.mycompany.view.web.facebookIntegration;

import com.facebook.api.FacebookParam;
import com.facebook.api.FacebookRestClient;

import javax.security.auth.login.FailedLoginException;

import org.apache.wicket.Request;

public class FaceBookAuthHandler {

    public static FacebookRestClient getAuthenticatedClient(Request request, String apiKey, String secretKey) throws Exception {
        String authToken = request.getParameter("auth_token");
        String sessionKey = request.getParameter(FacebookParam.SESSION_KEY.toString());
        FacebookRestClient fbClient = null;
        if (sessionKey != null) {
            fbClient = new FacebookRestClient(apiKey, secretKey, sessionKey);
        } else 
        	if (authToken != null) {
        		fbClient = new FacebookRestClient(apiKey, secretKey);
        		//	establish session
        		fbClient.auth_getSession(authToken);
        	} 
        	else {throw new FailedLoginException("Session key not found");}
        fbClient.setIsDesktop(false);
        return fbClient;
    }
}